require("gip")
require("pakfile")

local srcDir = "e:\\bug"
local pakName = "e:\\tmp\\b2"

gip.createParentDir( pakName)
local pak = pakfile.open( pakName, "wm")
if pak == nil then
    print( "open " .. pakName .. " failed.")
    return
end

function cutPath( filename)
    if string.find( filename, srcDir) ~= 1 then
        return failename
    else
        return string.sub( filename, #srcDir + 2)
    end
end

function transCallback( filename, isFile)
    if not isFile then
        return
    end
    
    local file = io.open( filename, "rb")
    if file == nil then
        print( "open " .. filename .. " failed.")
        return
    end
    
   
    local path = cutPath( filename)
    local pf = pak.open( path, "w")
    if pf == nil then
        print( "create pakfile " .. path .. " failed")
        file:close()
    end    
  
    local t = file:read("*all")
    file:close()
    
    pf.write( t)    
    if pf.close() ~= 0 then
        print( "flush " .. path .. "failed.")
    end    
end

gip.transDir( srcDir, transCallback, true)
pak.close()

    